package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.ReciverAddressDao;
import com.jbp.exception.RRException;
import com.jbp.model.ReciverAddress;
import com.jbp.service.ReciverAddressService;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import java.util.Map;

/**
 * 收款账号(ReciverAddress)表服务实现类
 * @author cc
 * @since 2020-06-20 23:35:26
 */
@Service
public class ReciverAddressServiceImpl extends ServiceImpl<ReciverAddressDao, ReciverAddress> implements ReciverAddressService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ReciverAddress> page = this.page(
                new Query<ReciverAddress>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    ///保存收款地址
    @Override
    public void saveReciverAddress(Integer userId, ReciverAddress req) {
        ///是否绑定过
        if (isBind(userId)) {
            throw new RRException("已经绑定过了");
        }
        ///保存认证信息
        ReciverAddress reciverAddress = new ReciverAddress();
        reciverAddress.setUserId(userId);
        reciverAddress.setAddress(req.getAddress());
        save(reciverAddress);
    }

    ///是否绑定过收款地址
    @Override
    public boolean isBind(Integer userId) {
        QueryWrapper<ReciverAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        return count(queryWrapper)>0;
    }
}